<TITLE>Workflow.NET
	<%#actionname%>
	-<%#propertyname%></TITLE>
<STYLE><%#stylesheet%></STYLE>
<BODY marginheight="0" marginwidth="0" topmargin="0" leftmargin="0" width="300">
	<form name="wf_theform">
		<TABLE width="100%" height="100%" bgcolor="<%#bgcolor%>" cellspacing="0" cellpadding="0" ID="Table1">
			<TR>
				<TD valign="top">
					<TABLE>
						<TR>
							<TD>
								<B>
									<%#actionname%>
									-</B><%#propertyname%>
							</TD>
						</TR>
						<TR>
							<TD>
								<input name="wf_addrow" type="button" class="buttons" onclick="addnewrow();" value="New row">
								<input name="wf_removerow" type="button" class="buttons" onclick="removeselected();" value="Remove selected">
							</TD>
						</TR>
						<TR>
							<TD height="<%#gridheight%>" valign="top" width="<%#gridwidth%>">
								<div id="divgrid" style="top:40">&nbsp;</div>
							</TD>
						</TR>
					</TABLE>
					<BR>
				</TD>
			</TR>
			<TR>
				<TD height="1" bgcolor="black"><IMG src="?drawimage=spacer.gif" height="1"></TD>
			</TR>
			<TR>
				<TD height="1" bgcolor="white"><IMG src="?drawimage=spacer.gif" height="1"></TD>
			</TR>
			<TR>
				<TD align="right" height="30">
					<input type="button" class="buttons" value="Update" style="width:<%#buttonwidth%>" name="btnok" onclick="submitform();" ID="Button1">
					<input type="button" class="buttons" value="Close" style="width:<%#buttonwidth%>" ID="btnCancel" onclick="window.close();" NAME="btnCancel">
				</TD>
			</TR>
		</TABLE>
	</form>
</BODY>
<SCRIPT>
	//variable declaration for scroll
	var obj;
	var height=255;
	var imgtop=40;
	var visiblewidth = <%#gridwidth%> + 20;
	var visible=<%#gridheight%>;
	var visibletop=0;
	var slidespeed =10;
	var visibleheight=<%#gridheight%>;
	var stopslidingup=false;
	var stopslidingdn=false;
	//*************************************

	var errstr="";
	var rowempty=false;
	var cols= new Array(0);
	var rows= new Array(0);
	var CustomControls = new Array();
	var gridwidth = <%#gridwidth%>;
	var mandatory="<%#mandatoryvalue%>";

	<%#addcol%>
	<%#addrow%>
	DrawGrid();


	function findObj(n, d)
	{
		var p,i,x;
		if(!d) d=document;
		if((p=n.indexOf("?"))>0&&parent.frames.length)
		{
			d=parent.frames[n.substring(p+1)].document;
			n=n.substring(0,p);
		}
		if(!(x=d[n])&&d.all) x=d.all[n];
		for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
		for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
		if(!x && d.getElementById) x=d.getElementById(n);
		return x;
	}


	function GetControlText(ControlName, ControlType, Width, TabIndex, Values,defaultvalue)
	{
		var controltext="";
		var valuearray;
		var i;

		var widthstr;

		if(Width > 0)
			widthstr = "width: "+Width;
		else
			widthstr = "";

		switch(ControlType)
		{
			case "select":
				controltext = "<SELECT name=\""+ControlName+"\" class=gridtableinputs style='"+widthstr+"' TABINDEX='"+TabIndex+"'>";
				var selectvalues=Values.split(",");

				for(i=0;i<selectvalues.length;i++)
				{
					valuearray = selectvalues[i].split(":");
					if (defaultvalue == valuearray[1])
						controltext	+= "<OPTION value=\"" + valuearray[1] + "\" selected>" + valuearray[0] + "</OPTION>";
					else
						controltext	+= "<OPTION value=\"" + valuearray[1] + "\">" + valuearray[0] + "</OPTION>";
				}
				controltext += "</SELECT>";
				break;
			case "variable":
				controltext ="<SELECT name=\""+ControlName+"\" class=gridtableinputs style='"+widthstr+"' TABINDEX='"+TabIndex+"'>";
				controltext += "<%#addvarcol%>";
				controltext +=	"</SELECT>";
				break;
			default:
				controltext ="<input name=\""+ControlName+"\" class=gridtableinputs type=textbox style='"+widthstr+"' TABINDEX='"+TabIndex+"' value='"+ defaultvalue+"'>";
				break;

		}

		return controltext;
	}

	function getcustomcontrolindex(controlname)
	{
		var returnvalue=-1;
		var i=0;
		for(i=0;i<CustomControls.length;i++)
		{
			if (CustomControls[i].name == controlname)
				returnvalue = i;
		}
		return returnvalue;
	}

	function row()
	{
		this.selected =false;
		this.cols = new Array(cols.length);
	}

	function col()
	{
		this.titlestring = "";
		this.width=0; //Max width of the field
		this.type="";
		this.values="";
		this.mandatory=false;
		this.displaywidth=""; //Display width for the column (to be specified with the TD tag)
		this.validationtype="";
	}

	function AddColumn(titlestring,width,type,values,mandatory,displaywidth,validationtype,defaultvalue)
	{
		var arrayindex = 0;
		arrayindex  = cols.length;
		cols[arrayindex] = new col();
		cols[arrayindex].titlestring = titlestring;

		//if( parseInt(width) >= 100 )
		//	cols[arrayindex].width = 0;
		//else
		//	cols[arrayindex].width = (parseInt(gridwidth)) * parseInt(width) / 100;

		cols[arrayindex].width = parseInt(width);
		cols[arrayindex].displaywidth = displaywidth;
		cols[arrayindex].mandatory = mandatory;
		cols[arrayindex].validationtype = validationtype;
		cols[arrayindex].defaultvalue = defaultvalue;

		if( type != null )
		{
			if( type == "select" )
			{
				cols[arrayindex].type = type;
				cols[arrayindex].values=values;
			}
			if( type == "variable" )
			{
				cols[arrayindex].type = type;
			}

		}
	}

	function AddRow()
	{
		var newarrayid = rows.length;
		rows[newarrayid] = new row();
		for (i=0;i<cols.length;i++)
		{
			if((arguments.length-1) >= i)
			{
				rows[newarrayid].cols[i] = arguments[i];
			}
			else
			{
				rows[newarrayid].cols[i] = "";
			}
		}
	}

	function DrawGrid()
	{

		if(navigator.userAgent.indexOf("Netscape") >0 )
				document.all = document.getElementsByTagName("*");

		var obj = document.all["divgrid"];
		var controltext ="";
		var customcontrolindex;

		var innerhtml="";
		var colwidth=0;
		innerhtml = "<TABLE width=<%#gridwidth%> bgcolor=#808080 cellspacing=1>";

		innerhtml += "<TR bgcolor=#d4d0c8>";
		innerhtml += "<TD bgcolor=#d4d0c8 width=15>&nbsp;</TD>";

		for (i = 0;i<cols.length;i++)
		{
			innerhtml += "<TD width='"+cols[i].displaywidth+"'>";
			innerhtml += cols[i].titlestring;
			innerhtml +="</TD>";
		}
		innerhtml +="</TR>";
		for (i = 0;i<rows.length;i++)
		{
			innerhtml +="<TR bgcolor=white>";
			innerhtml += "<TD bgcolor=#d4d0c8 width=15>&nbsp;</TD>";
			for(j=0;j<cols.length;j++)
			{
				innerhtml += "<TD height=15 >";
				innerhtml += "<div id='row" + i +"col" + j +"'>" + GetDisplayString(i,j,rows[i].cols[j]) + "</div>";
				innerhtml += "</TD>";
			}
			innerhtml +="</TR>";
		}

		innerhtml += "<TR bgcolor=white>";
		innerhtml += "<TD bgcolor=#d4d0c8 width=15>&nbsp;</TD>";
		for (i = 0;i<cols.length;i++)
		{
			colwidth = cols[i].width;
			if (parseInt(colwidth) ==0)
				colwidth = parseInt(gridwidth) / (cols.length + 1);

			innerhtml += "<TD>";

			controltext=GetControlText("wf_gridinput" + i , cols[i].type, cols[i].width, i+1, cols[i].values,cols[i].defaultvalue);

			innerhtml += controltext;

			innerhtml +="</TD>";
		}
		innerhtml +="</TR>";
		innerhtml +="</TABLE>";
		obj.innerHTML = innerhtml;
		height = (rows.length * 15) + 60;

		window.setTimeout('findObj("wf_gridinput0").focus();',250);
	}

	function GetDisplayString(rowid,colid,string)
	{
		var colwidth = cols[colid].width;
		var displaystring =string;
		/*if (parseInt(colwidth) ==0)
			colwidth = parseInt(gridwidth) / (cols.length + 1);

		var returnvalue="";
		colwidth = parseInt(colwidth/7)-1;
		if (string.length > colwidth )
		{
			displaystring = displaystring.substring(0,colwidth) + "...";
		}*/

		if (string.length > 0)
			returnvalue = "<A title='" + string + "' class=gridanchor href='javascript:selectrow("+ rowid +")'>" + displaystring + "</A>";
		else
			returnvalue = "&nbsp;";
		return returnvalue;
	}

	function addnewrow()
	{
		if( !checkandaddvalues() )
		{
			DrawGrid();
		}
		else
		{
			alert(errstr);
			window.setTimeout('findObj("wf_gridinput0").focus();',250);
		}

	}

	function removeselected()
	{
		if(navigator.userAgent.indexOf("Netscape") >0 )
							document.all = document.getElementsByTagName("*");

		obj = document.all["wf_gridinput0"];
		if (obj.value.length > 0 )
		{
			checkandaddvalues()
		}
		var temprows=new Array();
		var arrayindex = 0;
		for (i=0;i<rows.length;i++)
		{
			if (! rows[i].selected)
			{
				temprows[temprows.length] = rows[i];
			}
		}
		rows = temprows;
		DrawGrid();
	}

	function checkandaddvalues()
	{
		var temprow = new row();
		var flag = false;
		var err=false;

		errstr="";
		if(navigator.userAgent.indexOf("Netscape") >0 )
			document.all = document.getElementsByTagName("*");

		var obj;
		for (i=0;i<cols.length;i++)
		{
			obj = document.all["wf_gridinput" + i];
			//alert(cols[i].validationtype)
			var objValue = obj.value;
			if (objValue.length > 0 )
				{
					flag=true;
					if( cols[i].validationtype.length > 0 && cols[i].validationtype == "timespan")
					{
						var toMatchstr = obj.value
						if (toMatchstr.indexOf(".") < 0 )
							toMatchstr += ".00:00:00"

						var timeSpanPat = /^\d*\.[0-2]{1}[0-3]{1}\:[0-5]{1}[0-9]{1}\:[0-5]{1}[0-9]{1}/;
						var timeSpanPat1 = /^\d*\.[0-1]{1}[0-9]{1}\:[0-5]{1}[0-9]{1}\:[0-5]{1}[0-9]{1}/;
						var matchArray = toMatchstr.match(timeSpanPat);
						var matchArray1 = toMatchstr.match(timeSpanPat1);
						if(matchArray == null && matchArray1 == null)
						{
							err=true;
							errstr += "Not a valid Time Span (dd.hh:mm:ss) Value "+ cols[i].titlestring +"\r\n";
						}
					}
				}
			else

				if( cols[i].mandatory)
				{
					err=true;
					errstr += "Please specify value for "+cols[i].titlestring+"\r\n";
				}


			if(objValue.indexOf('<') >= 0 || objValue.indexOf('>') >=0)
			{
				err=true;
				errstr += "Please specify valid value for "+cols[i].titlestring+"\r\n";
			}
			temprow.cols[i] = objValue;
		}
		if (flag && !err)
			rows[rows.length] = temprow;

		//if(rows.length == 0 && !flag)
		//	flag = false;

		rowempty = !flag;

		if(mandatory =="True" && rows.length == 0)
			{
				rowempty=false;
				err=true;
				errstr += "Atleast one row needs to be defined.\r\n";
			}
		return (err);

	}

	function selectrow(rowid)
	{
		var obj
		for (i=0;i<cols.length;i++)
		{
			obj = document.all["row"+rowid + "col" + i];
			if (rows[rowid].selected)
				obj.style.backgroundColor = "white";
			else
				obj.style.backgroundColor = "#DBEAF5";
		}
		rows[rowid].selected  = !rows[rowid].selected;
	}

	function getgridvalues()
	{
		var returnstring="";
		var returnsubstring = "";

		returnstring += ConvertToString(rows.length,4);
		for (i=0;i<rows.length;i++)
		{
			returnsubstring = "";
			returnsubstring += ConvertToString(cols.length,4);
			for (j=0;j<cols.length;j++)
			{
				returnsubstring += "$" + ConvertToString(rows[i].cols[j].length,4)+rows[i].cols[j] ;
			}

			returnsubstring = ConvertToString( returnsubstring.length, 4 )+returnsubstring;
			returnstring +="#" + returnsubstring;
		}


		return returnstring;
	}

	function ConvertToString(  number,  width )
	{
		var str="";
		var i;

		str=number.toString();
		var l = str.length;
		for(i=0;i<(width-l);i++)
		{
			str = "0" + str;
		}
		return str.substring(0,width);
	}

	function submitform()
	{
		var err=checkandaddvalues();
		if( (rowempty) || (!rowempty && !err) )
		{
			window.opener.properties("_hidtempstore",getgridvalues());
			window.opener.submitform("?mode=edit&<%#qspropertypagename%>=<%#propertypage%>&<%#qspropertyname%>=<%#propertyname%>&<%#SelectedItemQueryString%>");
			window.close();
		}
		else
		{
			alert(errstr);
			window.setTimeout('findObj("wf_gridinput0").focus();',250);
		}
	}



</SCRIPT>
